import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex)

export default new Vuex.Store({
    plugins: [
        createPersistedState({ storage: window.sessionStorage }) //配置为sessionStorage，如果不传入对象就是默认
    ],
    state: {
        baseurl: "",        // 服务端基础路径
        userdata: null,     // 用户信息
        token: '',      // token
        isCollapse: false,  // 默认左侧导航栏的开启状态 默认开启
        titleObj: {      // 面包削基本信息
            title1: "就业升学信息",
            title2: "就业升学表格",
        },

        current: {
            path: "/workform",
            title: '就业升学表格'
        }, // 当前浏览页
        browseList: [{
            path: "/workform",
            title: '就业升学表格'
        }]       // 所有的浏览历史页
    },
    mutations: {
        // 设置面包削信息
        settitleObj(state, val) {
            state.titleObj = val
        },
        //清除选项栏(other)
        delothersbrowseList(state, val) {
            if (state.current.path == '/workform') {
                state.browseList = [{
                    path: "/workform",
                    title: '就业升学表格'
                }]
            } else {
                state.browseList = [{
                    path: "/workform",
                    title: '就业升学表格'
                }]

                state.browseList.push(state.current)
            }
        },
        // 清除选项栏(all)
        delallbrowseList(state, val) {

            state.browseList = [{
                path: "/workform",
                title: '就业升学表格'
            }]
            state.current = {
                path: "/workform",
                title: '就业升学表格'
            }
        },
        delcurrent(state, val) {
            let index = state.browseList.findIndex(item => {
                return item.path == val.path
            })
            if (val.path == state.current.path) {       // 当前删除项是当前的页面，定位到默认页去
                // 跳转到 升学就业表格 页面
                state.current = {
                    title: "升学就业表格",
                    path: "/workform",
                }


            }
            state.browseList.splice(index, 1)
        },
        setCurrent(state, val) {
            state.current = val
            let isTrue = false
            state.browseList.forEach(item => {
                if (item.path == val.path) {
                    isTrue = true         // 表示已经存在
                }
            })

            if (!isTrue) {
                if (val.path != "/login") {
                    state.browseList.push(val)
                }

            }
        },
        setisCollapse(state, val) {
            state.isCollapse = val
        },
        setbaseurl(state, val) {
            state.baseurl = val
        },
        setuserdata(state, val) {
            state.userdata = val
        },
        settoken(state, val) {
            state.token = val
        }
    },
    getters: {
        isShow(state) {
            if (state.userdata.shengfen !== 0) {
                return true
            } else {
                return false
            }
        },
        activeRouter(state) {
            return state.current.path
        }
    },
    actions: {
    },
    modules: {
    }
})




